/**
* All Rights Reserved , Copyright (C) 2023 , 青岛鼎信通讯股份有限公司
*
* MpmProjectProductInfoHisService
* 产品信息历史表
*
* 修改纪录
* 2023-04-23 版本：1.0 houyuhang 创建。
* @version 版本：1.0
* @author 作者：houyuhang
* 创建日期：2023-04-23
*/
package com.topscomm.mpm.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.topscomm.mpm.pojo.MpmProjectEntity;
import com.topscomm.mpm.pojo.MpmProjectProductInfoEntity;
import com.topscomm.mpm.pojo.MpmProjectProductInfoHisEntity;
import com.topscomm.pub.util.ConvertUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import com.topscomm.mpm.service.IMpmProjectProductInfoHisService;
import com.topscomm.mpm.service.impl.auto.MpmProjectProductInfoHisServiceAuto;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service(value = "mpmProjectProductInfoHisService")
@Scope(value = "singleton")
public class MpmProjectProductInfoHisService extends MpmProjectProductInfoHisServiceAuto implements IMpmProjectProductInfoHisService {
    @Autowired
    private MpmProjectProductInfoService mpmProjectProductInfoService;
    @Autowired
    private MpmProjectService mpmProjectService;
    /**
     * @author: houyuhang
     * @date: 2023-04-24
     * @description: 保存productinfo历史表
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void removeAndAddRecord(Map<String, Object> paraMap) {
        if (!paraMap.containsKey("productlist") && paraMap.get("productlist") == null) {
            return;
        }
        // 删除原有记录,生成新记录
        this.deleteByWhere(" changeid = '" + ConvertUtil.convertToString(paraMap.get("id")) + "' ");
        JSONArray jsonArray = (JSONArray) paraMap.get("productlist");
        List<Map> list = JSONObject.parseArray(jsonArray.toJSONString(), Map.class);
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        // 获取项目信息
        MpmProjectEntity mpmProjectEntity = mpmProjectService.queryObjectByWhere(" projectcode = '" + ConvertUtil.convertToString(paraMap.get("projectcode2")) + "' ");
        for (Map product : list) {
            MpmProjectProductInfoHisEntity mpmProjectProductInfoEntity = new MpmProjectProductInfoHisEntity();
            mpmProjectProductInfoEntity.convertFromMap(product);
            mpmProjectProductInfoEntity.setId(0L);
            mpmProjectProductInfoEntity.setProjectid(mpmProjectEntity.getId());
            mpmProjectProductInfoEntity.setProjectname(ConvertUtil.convertToString(paraMap.get("projectname2")));
            mpmProjectProductInfoEntity.setProjectcode(ConvertUtil.convertToString(paraMap.get("projectcode2")));
            mpmProjectProductInfoEntity.setChangeid(ConvertUtil.convertToLong(paraMap.get("id")));
            this.insert(mpmProjectProductInfoEntity);
        }
    }
    /**
     * @author: houyuhang
     * @date: 2023-04-23
     * @description: 将主表productrange字段同步到productinfo子表
     * @modify:
     */
    @Transactional(rollbackFor = Exception.class)
    public void insertSubProductInfo(Map<String, Object> changeMap) {
        // 获取产品信息
        List<Map> list = JSONObject.parseArray(ConvertUtil.convertToString(changeMap.get("productrange2")), Map.class);
        if (CollectionUtil.isEmpty(list)) {
            return;
        }
        for (Map tempMap : list) {
            Map<String, Object> sqlMap = new HashMap<>();
            sqlMap.put("projectsource", ConvertUtil.convertToString(changeMap.get("projectsource2")));
            sqlMap.put("productname", ConvertUtil.convertToString(tempMap.get("productType")));
            List<Map<String, Object>> productcodeList = mpmProjectProductInfoService.queryMap(sqlMap, "queryProductCode");
            MpmProjectProductInfoHisEntity productInfoEntity = new MpmProjectProductInfoHisEntity();
            productInfoEntity.setFund(ConvertUtil.convertToBigDecimal(tempMap.get("productMoney")));
            productInfoEntity.setProductlinecode(ConvertUtil.convertToString(tempMap.get("line")));
            productInfoEntity.setProductlinename(ConvertUtil.convertToString(tempMap.get("productLine")));
            if (!CollectionUtil.isEmpty(productcodeList)) {
                productInfoEntity.setProductcode(ConvertUtil.convertToString(productcodeList.get(0).get("code")));
            } else {
                // 根据业务方提供的对应关系赋值编码
                productInfoEntity.setProductcode(mpmProjectProductInfoService.getProductCodeOther(ConvertUtil.convertToString(changeMap.get("projectsource2")), ConvertUtil.convertToString(tempMap.get("productType"))));
            }
            productInfoEntity.setProductname(ConvertUtil.convertToString(tempMap.get("productType")));
            productInfoEntity.setProjectcode(ConvertUtil.convertToString(changeMap.get("projectcode2")));
            productInfoEntity.setProjectname(ConvertUtil.convertToString(changeMap.get("projectname2")));
            productInfoEntity.setProjectid(ConvertUtil.convertToLong(changeMap.get("projectid")));
            productInfoEntity.setChangeid(ConvertUtil.convertToLong(changeMap.get("id")));
            this.insert(productInfoEntity);
        }
    }
}
